local liangyin = fk.CreateSkill {

  name = "joy__liangyin",

  tags = {  },

}



liangyin:addEffect(fk.AfterCardsMove, {
  name = "joy__liangyin",
  mute = true,
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(liangyin.name) then
      for _, move in ipairs(data) do
        if move.toArea == Card.PlayerSpecial then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea ~= Card.PlayerSpecial then
              return true
            end
          end
        end
        if move.toArea == Card.PlayerHand then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerSpecial then
              return table.find(player.room.alive_players, function(p)
                return  not p:isNude() end)
            end
          end
        end
      end
    end
  end,
  on_trigger = function(self, event, target, player, data)
    local dat = ""
    for _, move in ipairs(data) do
      if move.toArea == Card.PlayerSpecial then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea ~= Card.PlayerSpecial then
            dat = "drawcard"
          end
        end
      end
      if move.toArea ~= Card.PlayerSpecial then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerSpecial then
            dat = "discard"
          end
        end
      end
    end
    if dat ~= "" then
      self:doCost(event, target, player, dat)
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets
    if data == "drawcard" then
      targets = table.map(room.alive_players, Util.IdMapper)
    elseif data == "discard" then
      targets = table.map(table.filter(room.alive_players, function(p)
        return not p:isNude() end), Util.IdMapper)
    end
    local to = room:askForChoosePlayers(player, targets, 1, 1, "#joy__liangyin-"..data, liangyin.name, true)
    if #to > 0 then
      event:setCostData(self, to[1])
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(event:getCostData(self))
    if data == "drawcard" then
      room:notifySkillInvoked(player, liangyin.name, "support")
      player:broadcastSkillInvoke(liangyin.name)
      to:drawCards(1, liangyin.name)
    elseif data == "discard" then
      room:notifySkillInvoked(player, liangyin.name, "control")
      player:broadcastSkillInvoke(liangyin.name)
      room:askForDiscard(to, 1, 1, true, liangyin.name, false)
    end
  end,
})

return liangyin